<template>
  <div class="add">
    <el-dialog
      title="用户注册"
      :visible="value"
      width="603px"
      height="786px"
      center
      @close="resclick()"
    >
      <el-form label-width="65px" :model="form" :rules="rules" ref="form">
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :action="baseUrl + '/uploads'"
            name="image"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="form.avatar"
              :src="baseUrl + '/' + form.avatar"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="昵称" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="图形码" prop="code">
          <el-row>
            <el-col :span="15">
              <el-input v-model="form.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="code-img"
                @click="refreshCode"
                :src="codeUrl"
                alt=""
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="验证码" prop="rcode">
          <el-row>
            <el-col :span="15">
              <el-input v-model="form.rcode"></el-input>
            </el-col>
            <el-col :span="8">
              <el-button class="code-img">获取用户验证码</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <div class="footer">
          <template footer>
            <el-button @click="resclick">取消</el-button>
            <el-button type="primary" @click="btnClick">确认</el-button>
          </template>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { register } from '@/api/login.js'
export default {
  // props: {
  //   value: {
  //     type: Boolean, // 类型
  //     default: true
  //   }
  // },
  props: ['value'],
  data () {
    return {
      codeUrl: process.env.VUE_APP_URL + '/captcha?type=login',
      baseUrl: process.env.VUE_APP_URL,
      //   isShow: false,
      form: {
        avatar: '',
        username: '',
        email: '',
        phone: '',
        password: '',
        rcode: ''
      },
      rules: {
        avatar: [{ required: true, message: '请上传头像', trigger: 'change' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入邮箱'))
              }
            },
            trigger: 'change'
          }
        ],
        code: [{ required: true, message: '请输入图形码', trigger: 'change' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        rcode: [{ required: true, message: '请输入验证码', trigger: 'change' }]
      }
    }
  },
  methods: {
    handleAvatarSuccess (res) {
      window.console.log(res)
      this.form.avatar = res.data.file_path
      this.$refs.form.validateField(['avatar'])
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    refreshCode () {
      this.codeUrl =
        process.env.VUE_APP_URL + '/captcha?type=login&dfhdfh=' + Date.now()
    },
    btnClick () {
      this.$refs.form.validate(async result => {
        if (result) {
          const res = await register(this.form)
          //   console.log(res)
          if (res.data.code === 200) {
            this.$message.success('注册成功')
            this.$emit('input', false)
          } else {
            this.$message.error(res.data.message)
          }
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    resclick () {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        avatar: '',
        username: '',
        email: '',
        phone: '',
        password: '',
        rcode: ''
      }
    }
  }
}
</script>

<style lang="less" scoped>
.add {
  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 150px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  ::v-deep .el-dialog__header {
    background: #109dfa;

    span.el-dialog__title {
      color: #fff;
    }

    i.el-dialog__close.el-icon.el-icon-close {
      color: #fff;
    }
  }
  .code-img {
    margin-left: 18px;
    width: 100%;
    height: 40px;
  }
  .footer {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 200px;
    margin: auto;
  }
}
</style>
